package com.shopx.cm.service.impl;


import com.shopx.cm.dao.db2.SysMaterialUserDao;
import com.shopx.cm.dto.MaterialUserDTO;
import com.shopx.cm.dto.OrgDTO;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.exception.OriginException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.base.service.base.BaseService;

import javax.annotation.Resource;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import com.shopx.dal.redis.CacheServiceFactory;
import com.shopx.cm.bean.CmMaterial;
import com.shopx.cm.model.CmMaterialModel;
import com.shopx.cm.dao.db1.CmMaterialDao;
import com.shopx.cm.service.ICmMaterialService;

/**
 * TODO
 *
 * @author lituo
 * @className: CmMaterial
 * @date 2025-01-08
 */
@Service
@Slf4j
public class CmMaterialServiceImpl implements ICmMaterialService {

    @Resource
    private CmMaterialDao cmMaterialDao;

    @Resource
    private SysMaterialUserDao sysMaterialUserDao;

    @Resource
    private ISysSeqService sysSeqService;

    @Override
    public CmMaterialModel selectByPrimaryKey(CmMaterial cmMaterial) {
        if (cmMaterial == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        return cmMaterialDao.selectByPrimaryKey(cmMaterial);
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int insertSelective(CmMaterial cmMaterial) {
        if (cmMaterial == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmMaterialDao.insertSelective(cmMaterial);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CmMaterial cmMaterial) {
        if (cmMaterial == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmMaterialDao.updateByPrimaryKeySelective(cmMaterial);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int deleteByPrimaryKey(CmMaterial cmMaterial) {
        if (cmMaterial == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmMaterialDao.deleteByPrimaryKey(cmMaterial);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int selectCount(CmMaterialModel cmMaterial) {
        return cmMaterialDao.selectCount(cmMaterial);
    }

    @Override
    public List<CmMaterialModel> selectList(CmMaterialModel cmMaterial) {
        List<CmMaterialModel> list = cmMaterialDao.selectList(cmMaterial);
        return list;
    }

    @Override
    public PageDTO<CmMaterialModel> getListPage(CmMaterialModel cmMaterial) {
        if (cmMaterial == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        PageDTO<CmMaterialModel> pageDTO = new PageDTO<CmMaterialModel>();
        pageDTO.setiTotalDisplayRecords(selectCount(cmMaterial));
        pageDTO.setData(selectList(cmMaterial));
        return pageDTO;
    }

    /**
     * 同步erp数据
     */
    public void syncErpData(){
        log.info("CmMaterialService.syncErpDate.begin");
        List<MaterialUserDTO> list = sysMaterialUserDao.queryList();
        log.info("list.size:{}，准备同步数据",list.size());
        cmMaterialDao.truncateTable();
        log.info("清空数据完成");
        cmMaterialDao.insertBatch(list);
        log.info("同步数据完成");
    }

}

